const app = getApp()
import {
  list
} from '../../../../api/address'
const util = require('../../../../utils/util.js')

Page({
  data: {
    addressInfo: [],
    type: "",
    goods: "",
    isShow: false,
    qrcodeLogin: app.QRCODELOGIN,
    btnDisabled: false,
    uid: ''
  },
  empty() {
    this.data.goods = ''
    this.data.isShow = false
    this.setData({
      goods: this.data.goods,
      isShow: this.data.isShow
    })
    this.addressListRequest()
  },
  searchGoods: util.debounce(function (e) {
    this.addressListRequest(e[0].detail.value)
    if (e[0].detail.value != "") {
      this.data.isShow = true
    } else if (e[0].detail.value == "") {
      this.data.isShow = false
    }
    this.setData({
      isShow: this.data.isShow
    })
  }, 500),
  onLoad: function (options) {
    console.log(options, 'options');
    if (options.btnDisabled) {
      this.setData({
        btnDisabled: options.btnDisabled
      })
    }
    if (!util.isNull(options.id)) {
      this.setData({
        uid: options.id
      })
    }
    if (options.type == "product") {
      this.data.type = options.type
    }
  },
  onShow: function () {
    this.addressListRequest()
  },
  addressListRequest(keyword) {
    let addressInfo = []
    let params = {
      keyword: keyword
    }
    if (!util.isNull(this.data.id)) {
      params.memberId = this.data.uid
    }
    list(params).then(res => {
      if (res.data.data.length > 0) {
        res.data.data.map((item) => {
          let addressInfoItem = {}
          addressInfoItem.id = item.id
          addressInfoItem.address = item.address
          addressInfoItem.areaName = item.areaName
          addressInfoItem.building = item.building
          addressInfoItem.roomNumber = item.roomNumber
          addressInfoItem.isDefault = item.isDefault
          addressInfoItem.phone = item.phone
          addressInfoItem.level = item.level
          addressInfoItem.lat = item.lat
          addressInfoItem.lng = item.lng
          addressInfoItem.memberId = item.memberId
          addressInfoItem.consignee = item.consignee
          addressInfoItem.addressDetail = item.address
          addressInfo.push(addressInfoItem)
        })
      }
      this.setData({
        addressInfo: addressInfo
      })
    })
  },
  //编辑收货地址的事件
  editAddress: function (e) {
    let id = e.currentTarget.dataset.id
    let addressDetail = {}
    if (this.data.addressInfo.length > 0) {
      this.data.addressInfo.map((item) => {
        if (item.id == id) {
          addressDetail = item
        }
      })
    }

    wx.navigateTo({
      url: '/pages/functions/jumpPage/addressEdit/addressEdit?type=edit',
      success: function (res) {
        res.eventChannel.emit('sendAddressDetail', {
          type: "edit",
          data: addressDetail
        })
      },
    })
  },
  addAddress: function (e) {  //添加收货地址的事件
    console.info("开始添加地址")
    wx.navigateTo({
      url: '/pages/functions/jumpPage/addressEdit/addressEdit?type=add',
      success: function (res) {
        res.eventChannel.emit('sendAddressDetail', {
          type: "add"
        })
      },
    })
  },
  addressClick(data) {  //订单确认时点击获取收货地址信息
    console.log(data)
    var _this = this
    if (this.data.type == "product") {
      wx.navigateBack({
        delta: 1
      })
      const eventChannel = _this.getOpenerEventChannel()
      eventChannel.emit('acceptDataFromOpenedPage', {
        data: data.currentTarget.dataset.item
      });
    }
  }
})